package apiroute

import (
	"git.sr.ht/~mil/mobroute/src/utilfuncs"
)

func formatDiagnosticsRoute(legs *[]RouteLeg) *DiagnosticsRoute {
	secsForLegType := func(legs *[]RouteLeg, legType string) uint {
		var total uint = 0
		for _, l := range *legs {
			if l.LegType == legType {
				total += l.LegDurationS
			}
		}
		return total
	}

	extractNTransfers := func(legs *[]RouteLeg) uint {
		var nTransfers uint = 0
		for _, l := range *legs {
			if l.LegType == "transfer" {
				nTransfers += 1
			}
		}
		return nTransfers
	}

	sWalk := secsForLegType(legs, "walk")
	sTransfer := secsForLegType(legs, "transfer")
	sTrip := secsForLegType(legs, "trip")
	nTransfers := extractNTransfers(legs)

	return &DiagnosticsRoute{
		RoutediagNTransfers: nTransfers,

		RoutediagSTotal:    sWalk + sTransfer + sTrip,
		RoutediagSWalk:     sWalk,
		RoutediagSTransfer: sTransfer,
		RoutediagSTrip:     sTrip,

		RoutediagDTotal:    utilfuncs.DurationFormatSecs(sWalk + sTransfer + sTrip),
		RoutediagDWalk:     utilfuncs.DurationFormatSecs(sWalk),
		RoutediagDTransfer: utilfuncs.DurationFormatSecs(sTransfer),
		RoutediagDTrip:     utilfuncs.DurationFormatSecs(sTrip),
	}
}
